package controllers;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import database.dao.Impl.CommodityDaoImpl;
import entity.Commodity;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import services.LoginService;
import services.SystemContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;

/**
 * @ClassName AddCommodityWindowController
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/17 23:13
 */
public class AddCommodityWindowController implements Initializable {

    public AnchorPane addPane;
    public TextField name;
    public TextArea commit;
    public TextField price;
    public TextField number;
    public ImageView image;
    public JFXButton addPictureBtn;
    public JFXButton addCommodity;
    public JFXComboBox classifyComboBox;
    private String imgPath="";

    private double xOffset;
    private double yOffset;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ViewManager.CONTROLLER.put("AddCommodityWindowController",this);

        Stage stage= ViewManager.STAGE.get("AddCommodityStage");
        //拖放功能
        addPane.setOnMousePressed(event -> {
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();
            addPane.setCursor(Cursor.CLOSED_HAND);
        });
        addPane.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);
        });

        addPane.setOnMouseReleased(event -> {
            addPane.setCursor(Cursor.DEFAULT);
        });

        //限制只能输入数字
        number.setTextFormatter(new TextFormatter<String>(new UnaryOperator<TextFormatter.Change>() {
            @Override
            public TextFormatter.Change apply(TextFormatter.Change change) {
                String value = change.getText();
                if(value.matches("[0-9]*")){
                    return change;
                }
                return null;
            }
        }));
    }

    public void addPictureAct(ActionEvent actionEvent) {
        FileChooser fc=new FileChooser();
        fc.setTitle("设置图片");
        fc.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("图片类型","*.png","*.jpg","*.bmp")
        );
        File file=fc.showOpenDialog(ViewManager.STAGE.get("AddCommodityStage"));
        if(file!=null){
            Thread thread = new Thread() {
                @Override
                public void run() {
                    imgPath=file.getPath();
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                FileInputStream fis=new FileInputStream(file);
                                Image image0=new Image("File:"+file.getAbsolutePath(),130,130,false,true,true);
                                image.setImage(image0);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            };
            thread.start();
        }
    }

    public void addCommodityAct(ActionEvent actionEvent) {
        Commodity commodity=null;
        if(!name.equals("")&&classifyComboBox.getSelectionModel().getSelectedItem()!=null) {
            commodity = new Commodity(name.getText(), Float.parseFloat(price.getText()), Integer.parseInt(number.getText()), ((Label) classifyComboBox.getSelectionModel().getSelectedItem()).getText(), imgPath, commit.getText(), SystemContext.getInstance().getId());
            CommodityDaoImpl commodityDao = new CommodityDaoImpl();
            commodityDao.addCommodity(commodity);

            name.clear();
            price.clear();
            number.clear();
            commit.clear();
            imgPath="";
            image.setImage(null);
        }
    }

    public void closeWindow(ActionEvent actionEvent) {
        ViewManager.STAGE.get("AddCommodityStage").close();
        SystemContext.getInstance().refreshCommodityList();
    }
}
